package com.devilvan.betrice.config;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

/**
 * @author Evad.Wu
 * @Description Kafka 配置类
 * @date 2023-02-10
 */
@Configuration
public class BetriceKafkaConfig {
    @Value("${spring.cloud.stream.kafka.binder.brokers}")
    private String brokers;
    @Value("${spring.cloud.stream.kafka.binder.configuration.key-serializer}")
    private String keySerializer;
    @Value("${spring.cloud.stream.kafka.binder.configuration.value-serializer}")
    private String valueSerializer;

    /**
     * 封装producer bean，发送端可以直接使用
     */
    @Bean(value = "betriceKafkaProducer")
    public Producer<String, String> betriceKafkaProducer() {
        Properties prop = new Properties();
        prop.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, brokers);
        prop.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, keySerializer);
        prop.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, valueSerializer);
        prop.put(ProducerConfig.ACKS_CONFIG, "1");
        return new KafkaProducer<>(prop);
    }
}
